#include "SFML\Graphics.hpp"
#include "Factory.h"
#include "texturelibrary.h"
#include "InputManager.h"
#include "GameStateManager.h"
#include "GameStateEnum.h"
#include "Math.h"

#include "VisableObject.h"
#include "TestObject.h"
#include "PlayerController.h"
#include "Backdrop.h"
#include "Rock.h"
#include "Reeds.h"
#include "JellyFish.h"
#include "EdibleFish.h"
#include "EnemyFish.h"

#include "StartMenu.h"
#include "DisplayAtAllFish1.h"
#include "DisplayAtAllFish2.h"

#include <iostream>

using namespace sf;

int ScreenWidth = 1280;
int ScreenHeight = 720;


bool startMenuIsShown = false;
bool pauseMenuIsShown = false;
bool ateAllFishScreenShown = false;
bool ateAllFishScreen2Shown = false;
bool wasEatenScreenShown = false;
bool fishReturnScreenShown = false;


StartMenuManager* theStartMenu;
AteAllScreen* theAteAllScreen;
AteAllScreen2* theAteAllScreen2;


#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))

char *arrayText[] =
{
	"images/defaultImage.png",
	"images/FishImage.png",
	"images/enemyFishImage.png",
	"images/Scene.png",
	"images/rockImage.png",
	"images/reedsImage.png",
	"images/jellyfishImage.png",
	"images/fishImage.png",
	"images/skeletonImage.png",
	"images/ediblefishImage.png",
	"images/ButtonStart.png",
	"images/ButtonExit.png",
	"images/AteAllFishiesImage.png",
	"images/AteAllFishiesImage2.png",
	"images/PowerBarBackImage.png",
	"images/PowerBarFrontImage.png"
};

	EdibleFish* endFish1;
	EdibleFish* endFish2;
	EdibleFish* endFish3;
	EdibleFish* endFish4;
	EdibleFish* endFish5;
	EdibleFish* endFish6;
	PlayerController* player;


void SetUpEndFish()
{
	endFish1 = new EdibleFish(sf::Vector2f(-150.0f, 250.0f), 0.1f, 0.1f);
	endFish2 = new EdibleFish(sf::Vector2f(-100.0f, 200.0f), 0.1f, 0.15f);
	endFish3 = new EdibleFish(sf::Vector2f(-200.0f, 500.0f), 0.1f, 0.2f);
	endFish4 = new EdibleFish(sf::Vector2f(1350.0f, 100.0f), -0.1f, 0.1f);
	endFish5 = new EdibleFish(sf::Vector2f(1450.0f, 300.0f), -0.1f, 0.15f);
	endFish6 = new EdibleFish(sf::Vector2f(1650.0f, 480.0f), -0.1f, 0.2f);
}

void SetUpObjects()
{
	theStartMenu = new StartMenuManager();
	theAteAllScreen = new AteAllScreen();
	theAteAllScreen2 = new AteAllScreen2();

	player = new PlayerController(sf::Vector2f(600.0f, 300.0f));

	Reeds* reed7 =  new Reeds(sf::Vector2f(200.0f, 0), 1.5f,  ScreenHeight);
	Reeds* reed8 =  new Reeds(sf::Vector2f(410.0f, 0), 3.0f,  ScreenHeight);
	Reeds* reed9 =  new Reeds(sf::Vector2f(1000.0f, 0), 2.5f,  ScreenHeight);
	Reeds* reed10 = new Reeds(sf::Vector2f(1300.0f, 0), 1.0f, ScreenHeight);
	Reeds* reed11 = new Reeds(sf::Vector2f(1800.0f, 0), 1.5f, ScreenHeight);

	Rock* rock1  = new Rock(sf::Vector2f(100.0f, 0), -2.5f, ScreenHeight, true);
	Rock* rock2 = new Rock(sf::Vector2f(690.0f, 0), 1.0f, ScreenHeight, true);
	Rock* rock3 = new Rock(sf::Vector2f(880.0f, 0), -2.0f, ScreenHeight, true);
	Rock* rock4 = new Rock(sf::Vector2f(1120.0f, 0), 2.5f, ScreenHeight, true);
	Rock* rock5 = new Rock(sf::Vector2f(1450.0f, 0), -1.0f, ScreenHeight, true);
	Rock* rock6 = new Rock(sf::Vector2f(1700.0f, 0), -4.0f, ScreenHeight, true);

	Rock* rock7  = new Rock(sf::Vector2f(370.0f, 0), -3.0f, ScreenHeight, false);
	Rock* rock8  = new Rock(sf::Vector2f(510.0f, 0), 1.5f, ScreenHeight, false);
	Rock* rock9  = new Rock(sf::Vector2f(780.0f, 0), 3.5f, ScreenHeight, false);
	Rock* rock10  = new Rock(sf::Vector2f(1120.0f, 0), -3.0f, ScreenHeight, false);
	Rock* rock11 = new Rock(sf::Vector2f(1300.0f, 0), 1.5f, ScreenHeight, false);
	Rock* rock12 = new Rock(sf::Vector2f(1580.0f, 0), -3.5f, ScreenHeight, false);


	EdibleFish* fish1  = new  EdibleFish(sf::Vector2f(200.0f, 250.0f),  -0.2f, 0.9f);
	EdibleFish* fish2  = new  EdibleFish(sf::Vector2f(400.0f, 100.0f),	0.3f, 0.6f);
	EdibleFish* fish3  = new  EdibleFish(sf::Vector2f(600.0f, 275.0f), -0.1f, 0.2f);
	EdibleFish* fish4  = new  EdibleFish(sf::Vector2f(800.0f, 400.0f), -0.15f, 0.3f);
	EdibleFish* fish5  = new  EdibleFish(sf::Vector2f(1000.0f, 300.0f), 0.3f, 0.7f);
	EdibleFish* fish6  = new  EdibleFish(sf::Vector2f(1200.0f, 250.0f), -0.2f, 0.9f);
	EdibleFish* fish7  = new  EdibleFish(sf::Vector2f(1400.0f, 100.0f), 0.3f, 0.6f);
	EdibleFish* fish8  = new  EdibleFish(sf::Vector2f(1600.0f, 275.0f), -0.1f, 0.2f);
	EdibleFish* fish9  = new  EdibleFish(sf::Vector2f(1800.0f, 400.0f), -0.15f, 0.3f);
	EdibleFish* fish10 = new  EdibleFish(sf::Vector2f(2000.0f, 300.0f), 0.3f, 0.7f);
	EdibleFish* fish11 = new  EdibleFish(sf::Vector2f(2200.0f, 250.0f), -0.2f, 0.9f);
	EdibleFish* fish12 = new  EdibleFish(sf::Vector2f(2400.0f, 100.0f), 0.3f, 0.6f);



	BackDrop* backdrop = new BackDrop();
}



int main()
{

	//load textures into texture library
	int sizeofArrayText = ARRAY_SIZE(arrayText);
	for (int i = 0; i < sizeofArrayText; ++i)
	{
		TT::LoadTexture(arrayText[i]);
	}


	// Create render window
	VideoMode video(ScreenWidth, ScreenHeight);
	RenderWindow window(video, "Theres Always A Bigger Fish");

	// Create clock for Time and delta Time
	Clock deltaTimer, timer;

	SetUpObjects();

	// Main game loop:
	while (window.isOpen())
	{

		float deltaT = deltaTimer.getElapsedTime().asSeconds();
		deltaTimer.restart();
		InputManager::UpdateInputs(&window, deltaT);

		//
		//While in the start menu
		//
		if (GameStateManager::GetGameState() == StartMenu)
		{
			if (!startMenuIsShown)
			{
				//Draw Menu
				theStartMenu->DrawMenu(&window);
				startMenuIsShown = true;
			}
			
			//if one of the buttons is pressed
			if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
			{

				if (theStartMenu->StartButtonIsPressed((sf::Vector2f)Mouse::getPosition(window)))
				{
					startMenuIsShown = false;
					GameStateManager::SetGameState(Playing);
				}
				if (theStartMenu->ExitButtonIsPressed((sf::Vector2f)Mouse::getPosition(window)))
				{
					window.close();
				}
			}
			
		}
		//
		//While in game Mode
		//
		else
		{
			//
			//While Playing the game
			//
			if (GameStateManager::GetGameState() == Playing || GameStateManager::GetGameState() == PlayingFullyGrown)
			{
				// Calculate time:
				float time = timer.getElapsedTime().asSeconds();

				// Update
				Factory::UpdateAll(deltaT);

				//Render
				window.clear();
				Factory::RenderAll(&window);
				player->RenderPowerBar(&window);
				window.display();


				if (GameStateManager::GetNumOfFish() <= 0 && GameStateManager::GetGameState() == Playing)
				{
					GameStateManager::SetGameState(PlayingFullyGrown);
				}
			}
			//
			//if you get eaten
			//
			else if (GameStateManager::GetGameState() == PlayingDied)
			{

				


			}
			//
			//Once all the fish are eaten
			//
			else if (GameStateManager::GetGameState() == PlayingAteAllFish)
			{
				if (!ateAllFishScreenShown)
				{
					//Draw Menu
					theAteAllScreen->DrawMenu(&window);
					ateAllFishScreenShown = true;
				}

				//if one of the buttons is pressed
				if (InputManager::EnterPressed())
				{
					ateAllFishScreenShown = false;
					GameStateManager::SetGameState(PlayingAteAllFish2);
				}
			}
			//
			//Once all the fish are eaten Second Screen
			//
			else if (GameStateManager::GetGameState() == PlayingAteAllFish2)
			{
				if (!ateAllFishScreen2Shown)
				{
					Factory::RenderAll(&window);
					//Draw Menu
					theAteAllScreen2->DrawMenu(&window);
					ateAllFishScreen2Shown = true;
				}

				//if one of the buttons is pressed
				if (InputManager::EnterPressed())
				{
					ateAllFishScreen2Shown = false;
					GameStateManager::SetGameState(AteAllEnd);
					SetUpEndFish();
				}
			}
			//
			//Once all the fish are eaten and repopulatng
			//
			else if (GameStateManager::GetGameState() == AteAllEnd)
			{
				
				endFish1->pos.x = Lerp(endFish1->pos.x, 600.0f, 0.001f);
				endFish2->pos.x = Lerp(endFish2->pos.x, 500.0f, 0.001f);
				endFish3->pos.x = Lerp(endFish3->pos.x, 700.0f, 0.001f);
				endFish4->pos.x = Lerp(endFish4->pos.x, 300.0f, 0.001f);
				endFish5->pos.x = Lerp(endFish5->pos.x, 600.0f, 0.001f);
				endFish6->pos.x = Lerp(endFish6->pos.x, 800.0f, 0.001f);

				//Render
				window.clear();
				Factory::RenderAll(&window);
				window.display();

				//if one of the buttons is pressed
				if (InputManager::EnterPressed())
				{
					Factory::DeleteAll();
					GameStateManager::ResetNumFish();
					SetUpObjects();
					GameStateManager::SetGameState(StartMenu);
				}

			}
			//
			//While game is paused
			//
			else if (GameStateManager::GetGameState() == Paused)
			{
				if (!pauseMenuIsShown)
				{
					//Draw Menu
				}

				//if one of the buttons is pressed
			}
		}




			
	}
	return 0;
}


